using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Sinacor.Infra.Service.Data;
using Sinacor.Infra.Service.Security.Authorization;
using Sinacor.Infra.Service.Common;

namespace Sinacor.Servico.Bovespa.Corretagem.ResourceAccess
{

    /// <summary>
    /// Business Logic Verificacao
    /// </summary>
    public class Verificacao : BaseResourceAccess
    {
        #region Construtor

        /// <summary>
        /// Construtor padrão
        /// </summary>
        public Verificacao()
            : base()
        {
        }

        #endregion


        #region ConsultarVerificacao
        /// <summary>
        /// Método ConsultarVerificacao
        /// </summary>
        public List<BusinessEntity.Verificacao> ConsultarVerificacao(BusinessEntity.Parameter.ConsultarVerificacao consultarverificacao)
        {
            List<BusinessEntity.Verificacao> response = null;
            StringBuilder sql = new StringBuilder();

            sql.Append(@"select TBOCORR_VERI.CD_POLITICA, 
							TBOCORR_VERI.dt_inicial, 
							TBOCORR_VERI.CD_operador, 
							TBOCORR_VERI.CD_assessor, 
							TBOCORR_VERI.CD_agente, 
							TBOCORR_VERI.tp_mercado,
							TBOCORR_VERI.cd_carliq, 
							TBOCORR_VERI.CD_codisi,
							TBOCORR_VERI.CD_tabela, 
							TBOCORR_VERI.CD_tabela_Benef, 
							TBOCORR_VERI.cod_nat_oprc 
							from TBOCORR_VERI
							where TBOCORR_VERI.CD_POLITICA = :CD_POLITICA 
							and TBOCORR_VERI.dt_inicial = :DT_INICIAL");

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql.ToString()))
            {
                this.DataBase.AddInParameter(command, "CD_POLITICA", DbType.Int32, consultarverificacao.CodigoPoliticaCorretagem);
                this.DataBase.AddInParameter(command, "DT_INICIAL", DbType.DateTime, consultarverificacao.DataInicial);

                response = this.FindAll<BusinessEntity.Verificacao>(command, new ConstructEntity<BusinessEntity.Verificacao>(ConstrutorConsultarVerificacao));
            }

            return response;
        }

        #endregion

        #region ConstrutorConsultarVerificacao
        protected BusinessEntity.Verificacao ConstrutorConsultarVerificacao(IDataReader reader)
        {
            BusinessEntity.Verificacao entity = new BusinessEntity.Verificacao();

            entity.CodigoPoliticaCorretagem = reader.GetValue<System.Int32>("CD_POLITICA");
            entity.DataInicial = reader.GetValue<System.DateTime>("dt_inicial");
            entity.Operador = reader.GetValue<System.Int32>("CD_operador");
            entity.Assessor = reader.GetValue<System.Int32>("CD_assessor");
            entity.Agente = reader.GetValue<System.Int32>("CD_agente");
            entity.TipoMercado = EnumConverter.Parse<Sinacor.Servico.Bovespa.Corretagem.BusinessEntity.Enum.TipoMercado>(reader.GetValue<string>("tp_mercado"));
            entity.Carteira = reader.GetValue<System.Int32>("cd_carliq");
            entity.Ativo = reader.GetValue<System.String>("CD_codisi");
            entity.CodigoTabelaPadrao = reader.GetValue<System.Int32>("CD_tabela");
            entity.CodigoTabelaBeneficio = reader.GetValue<System.Int32>("CD_tabela_Benef");
            entity.TipoNaturezaOperacao = EnumConverter.Parse<Sinacor.Servico.Bovespa.Corretagem.BusinessEntity.Enum.TipoNaturezaOperacao>(reader.GetValue<string>("cod_nat_oprc"));

            return entity;
        }
        #endregion

        #region ExcluirVerificacao
        /// <summary>
        /// Método ExcluirVerificacao
        /// </summary>
        public void ExcluirVerificacao(BusinessEntity.Parameter.ExcluirVerificacao excluirverificacao)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append(@"delete from TBOCORR_VERI
							where cd_politica = :CD_POLITICA
							and dt_inicial = :DT_INICIAL");

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql.ToString()))
            {
                this.DataBase.AddInParameter(command, "CD_POLITICA", DbType.Int32, excluirverificacao.CodigoPoliticaCorretagem);
                this.DataBase.AddInParameter(command, "DT_INICIAL", DbType.DateTime, excluirverificacao.DataInicial);

                this.DataBase.ExecuteNonQuery(command);

            }


        }

        #endregion


        #region SalvarVerificacao
        /// <summary>
        /// Método SalvarVerificacao
        /// </summary>
        public void SalvarVerificacao(BusinessEntity.Verificacao verificacao)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append(@"insert into TBOCORR_VERI
							(CD_OPERADOR,
							TP_MERCADO,
							CD_POLITICA,
							DT_INICIAL,
							CD_ASSESSOR,
							CD_AGENTE,
							CD_CODISI,
							CD_CARLIQ,
							CD_TABELA,
							CD_TABELA_BENEF,
							COD_NAT_OPRC)
							values
							(:CD_OPERADOR,
							:TP_MERCADO,
							:CD_POLITICA,
							:DT_INICIAL,
							:CD_ASSESSOR,
							:CD_AGENTE,
							:CD_CODISI,
							:CD_CARLIQ,
							:CD_TABELA,
							:CD_TABELA_BENEF,
							:COD_NAT_OPRC)");

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sql.ToString()))
            {
                this.DataBase.AddInParameter(command, "CD_OPERADOR", DbType.Int32, verificacao.Operador);
                string enumTipoMercado = EnumConverter.ToString<Sinacor.Servico.Bovespa.Corretagem.BusinessEntity.Enum.TipoMercado?>(verificacao.TipoMercado);
                this.DataBase.AddInParameter(command, "TP_MERCADO", DbType.String, enumTipoMercado);
                this.DataBase.AddInParameter(command, "CD_POLITICA", DbType.Int32, verificacao.CodigoPoliticaCorretagem);
                this.DataBase.AddInParameter(command, "DT_INICIAL", DbType.DateTime, verificacao.DataInicial);
                this.DataBase.AddInParameter(command, "CD_ASSESSOR", DbType.Int32, verificacao.Assessor);
                this.DataBase.AddInParameter(command, "CD_AGENTE", DbType.Int32, verificacao.Agente);
                this.DataBase.AddInParameter(command, "CD_CODISI", DbType.String, verificacao.Ativo);
                this.DataBase.AddInParameter(command, "CD_CARLIQ", DbType.Int32, verificacao.Carteira);
                this.DataBase.AddInParameter(command, "CD_TABELA", DbType.Int32, verificacao.CodigoTabelaPadrao);
                this.DataBase.AddInParameter(command, "CD_TABELA_BENEF", DbType.Int32, verificacao.CodigoTabelaBeneficio);
                string enumTipoNaturezaOperacao = EnumConverter.ToString<Sinacor.Servico.Bovespa.Corretagem.BusinessEntity.Enum.TipoNaturezaOperacao?>(verificacao.TipoNaturezaOperacao);
                this.DataBase.AddInParameter(command, "COD_NAT_OPRC", DbType.String, enumTipoNaturezaOperacao);

                this.DataBase.ExecuteNonQuery(command);

            }


        }

        public void SalvarVerificacao(List<BusinessEntity.Verificacao> verificacao)
        {
            foreach (BusinessEntity.Verificacao item in verificacao)
            {
                SalvarVerificacao(item);
            }
        }

        #endregion

        #region ValidarAgente
        /// <summary>
        /// Método ValidarAgente
        /// </summary>
        public System.Boolean ValidarAgente(BusinessEntity.Verificacao verificacao)
        {
            throw new NotImplementedException("Rever implementação");
        }

        #endregion


        #region ValidarAssessor
        /// <summary>
        /// Método ValidarAssessor
        /// </summary>
        public System.Boolean ValidarAssessor(BusinessEntity.Verificacao verificacao)
        {
            throw new NotImplementedException("Rever implementação");
        }

        #endregion



        #region ValidarAtivo
        /// <summary>
        /// Método ValidarAtivo
        /// </summary>
        public System.Boolean ValidarAtivo(BusinessEntity.Verificacao verificacao)
        {
            throw new NotImplementedException("Rever implementação");
        }

        #endregion

        #region ValidarCodigoTabelaBeneficio
        /// <summary>
        /// Método ValidarCodigoTabelaBeneficio
        /// </summary>
        public System.Boolean ValidarCodigoTabelaBeneficio(BusinessEntity.Verificacao verificacao)
        {
            throw new NotImplementedException("Rever implementação");
        }

        #endregion

        #region ValidarCodigoTabelaPadrao
        /// <summary>
        /// Método ValidarCodigoTabelaPadrao
        /// </summary>
        public System.Boolean ValidarCodigoTabelaPadrao(BusinessEntity.Verificacao verificacao)
        {
            throw new NotImplementedException("Rever implementação");
        }

        #endregion

        #region ValidarOperador
        /// <summary>
        /// Método ValidarOperador
        /// </summary>
        public System.Boolean ValidarOperador(BusinessEntity.Verificacao verificacao)
        {
            throw new NotImplementedException("Rever implementação");
        }

        #endregion
        
    }
}



